package com.javasm.service.stockService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.stockCtriteria.OutStorageCriteria;
import com.javasm.domin.entity.stockEntity.OutBriefList;
import com.javasm.domin.entity.stockEntity.OutDetailedOrder;
import com.javasm.domin.vo.stockVo.OutBriefListVo;
import com.javasm.domin.vo.stockVo.OutDetailedInfoVo;
import com.javasm.mapper.stockMapper.OutBriefListMapper;
import com.javasm.mapper.stockMapper.OutDetailedOrderMapper;
import com.javasm.service.stockService.OutBriefListService;
import com.javasm.transfer.stockTransfer.OutBriefListTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
/**
 * @Author：zzt
 * @Version：1.0
 * @Date：2021/11/19-23:55
 * @Since:jdk1.8
 * @Description:
 */
public class OutBriefListServiceImpl extends ServiceImpl<OutBriefListMapper, OutBriefList> implements OutBriefListService {
    private final OutBriefListMapper outBriefListMapper;
    private final OutBriefListTransfer outBriefListTransfer;
    @Resource
    private OutBriefListService outBriefListService;
    @Resource
    private OutDetailedOrderMapper outDetailedOrderMapper;
    @Override
    public PageResult<OutBriefListVo> selectPage(OutStorageCriteria outStorageCriteria) {
        String salesOutOrderId = outStorageCriteria.getSalesOutOrderId();
        String orderType = outStorageCriteria.getOrderType();
        String payType = outStorageCriteria.getPayType();
        String vipAccount = outStorageCriteria.getVipAccount();
        String orderStatus = outStorageCriteria.getOrderStatus();
        Date orderDate = outStorageCriteria.getOrderDate();
        LocalDateTime startTime = outStorageCriteria.getStartTime();
        LocalDateTime endTime = outStorageCriteria.getEndTime();
        LambdaQueryWrapper<OutBriefList> lambda = new QueryWrapper<OutBriefList>().lambda()
                .and(!StringUtils.isEmpty(salesOutOrderId), adminLambdaQueryWrapper -> adminLambdaQueryWrapper.like(OutBriefList::getSalesOutOrderId,salesOutOrderId))
                .and(!StringUtils.isEmpty(orderType),adminLambdaQueryWrapper -> adminLambdaQueryWrapper.like(OutBriefList::getOrderType,orderType))
                .and(!StringUtils.isEmpty(payType),adminLambdaQueryWrapper1 -> adminLambdaQueryWrapper1.like(OutBriefList::getPayType,payType))
                .and(!StringUtils.isEmpty(vipAccount),adminLambdaQueryWrapper1 -> adminLambdaQueryWrapper1.like(OutBriefList::getVipAccount,vipAccount))
                .and(!StringUtils.isEmpty(orderStatus),adminLambdaQueryWrapper1 -> adminLambdaQueryWrapper1.like(OutBriefList::getOrderStatus,orderStatus))
                .between(!StringUtils.isEmpty(orderDate),OutBriefList::getOrderDate,startTime,endTime);
        PageHelper.startPage(outStorageCriteria.getCurrentPage(),outStorageCriteria.getPageSize());
        List<OutBriefList> brandList = outBriefListMapper.selectList(lambda);

        PageInfo<OutBriefList> outBriefListVoPageInfo = new PageInfo<>(brandList);
        return new PageResult<>(outBriefListVoPageInfo.getTotal(),outBriefListTransfer.toVO(brandList));
    }

    @Override
    public OutDetailedInfoVo detailedInfoById(Serializable id) {
        OutBriefList outBriefListInfo = outBriefListMapper.findOutBriefListInfo(id);
        OutDetailedInfoVo infoVo = new OutDetailedInfoVo();
        BeanUtils.copyProperties(outBriefListInfo,infoVo);
        return infoVo;
    }

    @Override
    public boolean updateOrderStatus(Serializable id) {
        return outBriefListMapper.updateOrderStatus(id);
    }
}
